<?php

namespace app\admin\controller\data;

use app\admin\model\Admin;
use app\admin\model\Shop;
use app\admin\model\Shopcharge;
use app\admin\model\Shopdevice;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproom;
use app\admin\model\Shoptableorder;
use app\admin\model\Tuanorder;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Analysis extends Backend
{
    protected $noNeedRight = ['ajaxData'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Shopdaytrade;

    }
    /**
     * 查看
     */
    public function index()
    {

        $this->request->filter(['strip_tags', 'trim']);

        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();




            $list = array();



            $result = array("total" => 0, "rows" => $list);

            return json($result);
        }



        return $this->view->fetch();
    }

    public function ajaxData()
    {

        $shop_id = $this->request->param('shop_id');
        $date = $this->request->param('date');

        if($date == '' || $shop_id == ''){
            $this->error("请选择日期或者店铺");
        }

        $date = explode(" - ",$date);
        $start = $date[0];
        $end = $date[1];

        $shopRoomModel = new Shoproom();

        $shopOrderModel = new Shoporder();
        $roomList = $shopRoomModel->where('shop_id',$shop_id)->select();
        foreach ($roomList as $key=>$val){
           $amount  =  $shopOrderModel->where('room_id',$val['id'])->where('status','in',['doing','done','payed'])->where("create_time",">=",strtotime($start))->where('create_time','<=',strtotime($end))->sum('amount');
           $count  =  $shopOrderModel->where('room_id',$val['id'])->where('status','in',['doing','done','payed'])->where("create_time",">=",strtotime($start))->where('create_time','<=',strtotime($end))->count('id');

            $roomList[$key]['amount'] = $amount;
            $roomList[$key]['total'] = $count;

        }

        $ret = array();
        $ret['shop_id'] = $shop_id;
        $ret['roomList'] = $roomList;


        return json($ret);


    }


}
